package com.hm.shopping.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.su.bean.ServerResponse;
import com.hm.shopping.auto.su.ControllerResult;
import com.hm.shopping.auto.su.R.Restful;
import com.hm.shopping.auto.su.helper.HmSessionFactory;
import com.hm.shopping.service.ClassificationService;
import com.hm.shopping.vo.ClassificationDetailVo;
import com.hm.shopping.vo.ClassificationVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年1月29日 下午2:23:13
 * @version 1.0
 * @describe 商品分类
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/classification")
public class ClassificationApi {

	@Autowired
	private ClassificationService classificationService;
	
	private static final String SWWAGER_TAG = "商品分类管理";
	
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页商品分类记录", httpMethod = "GET", notes = "com.hm.shopping.api.ClassificationApi::queryClassifications")
	public ResponseEntity<ServerResponse<PageInfo<ClassificationVo>>> queryClassifications(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") int pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") int pageSize,
			@ApiParam(required = false, name = "classificationName", value = "商品分类名称") @RequestParam(required = false, name = "classificationName") String classificationName){
		log.debug("用户{},查询分页商品分类记录", HmSessionFactory.currentUserId());
		return ControllerResult.success(classificationService.queryClassifications(new PageInfoParam(pageNumber, pageSize), classificationName));
	}
	
	@RequestMapping(value = "/detail/list/{classificationId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页商品分类明细记录", httpMethod = "GET", notes = "com.hm.shopping.api.ClassificationApi::getClassificationDetailList")
	public ResponseEntity<ServerResponse<List<ClassificationDetailVo>>> getClassificationDetailList(
			@ApiParam(required = true, name = "classificationId", value = "商品分类ID") @PathVariable String classificationId){
		return ControllerResult.success(classificationService.getClassificationDetailList(classificationId));
	}

	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "新增商品分类信息", httpMethod = "POST", notes = "com.hm.shopping.api.ClassificationApi::addClassification")
	public ResponseEntity<ServerResponse<Boolean>> addClassification(
			@ApiParam(required = true, name = "body") @RequestBody(required = true) ClassificationVo classificationVo) {
		log.debug("用户{}新增商品分类信息", HmSessionFactory.currentUserId());
		classificationVo.setCreateUser(HmSessionFactory.currentUserId());
		classificationService.addClassification(classificationVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/{classificationId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取商品分类信息", httpMethod = "GET", notes = "com.hm.shopping.api.ClassificationApi::getClassification")
	public ResponseEntity<ServerResponse<ClassificationVo>> getClassification(
			@ApiParam(required = true, name = "classificationId", value = "商品分类ID") @PathVariable("classificationId") String classificationId) {
		log.debug("用户{}获取商品分类信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(classificationService.getClassification(classificationId));
	}

	@RequestMapping(value = "/{classificationId}", method = RequestMethod.PUT)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改商品分类信息", httpMethod = "PUT", notes = "com.hm.shopping.api.ClassificationApi::updateClassification")
	public ResponseEntity<ServerResponse<Boolean>> updateClassification(
			@ApiParam(required = true, name = "classificationId", value = "商品分类ID") @PathVariable("classificationId") String classificationId,
			@ApiParam(required = true, name = "body") @RequestBody(required = true) ClassificationVo classificationVo) {
		log.debug("用户{}修改商品分类信息", HmSessionFactory.currentUserId());
		classificationVo.setId(classificationId);
		classificationService.updateClassification(classificationVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@ApiOperation(tags = SWWAGER_TAG, value = "删除商品分类信息", httpMethod = "DELETE", notes = "com.hm.shopping.api.ClassificationApi::deleteClassification")
	public ResponseEntity<ServerResponse<Boolean>> deleteClassification(
			@ApiParam(required = true, name = "classificationId", value = "商品分类ID") @RequestParam(required = true, name = "classificationId") String classificationId) {
		log.debug("用户{}删除商品分类信息", HmSessionFactory.currentUserId());
		classificationService.deleteClassification(classificationId);
		return ControllerResult.success(true);
	}
	
	//----------分类明细----------
	
	@RequestMapping(value="/detail",method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "新增商品分类明细信息", httpMethod = "POST", notes = "com.hm.shopping.api.ClassificationApi::addClassificationDetail")
	public ResponseEntity<ServerResponse<Boolean>> addClassificationDetail(
			@ApiParam(required = true, name = "body") @RequestBody(required = true) ClassificationDetailVo classificationDetailVo) {
		log.debug("用户{}新增商品分类明细信息", HmSessionFactory.currentUserId());
		classificationDetailVo.setCreateUser(HmSessionFactory.currentUserId());
		classificationService.addClassificationDetail(classificationDetailVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/detail/{classificationDetailId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取商品分类明细信息", httpMethod = "GET", notes = "com.hm.shopping.api.ClassificationApi::getClassificationDetail")
	public ResponseEntity<ServerResponse<ClassificationDetailVo>> getClassificationDetail(
			@ApiParam(required = true, name = "classificationDetailId", value = "商品分类ID") @PathVariable("classificationDetailId") String classificationDetailId) {
		log.debug("用户{}获取商品分类明细信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(classificationService.getClassificationDetail(classificationDetailId));
	}

	@RequestMapping(value = "/detail/{classificationDetailId}", method = RequestMethod.PUT)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改商品分类明细信息", httpMethod = "PUT", notes = "com.hm.shopping.api.ClassificationApi::updateClassificationDetail")
	public ResponseEntity<ServerResponse<Boolean>> updateClassificationDetail(
			@ApiParam(required = true, name = "classificationDetailId", value = "商品分类ID") @PathVariable("classificationDetailId") String classificationDetailId,
			@ApiParam(required = true, name = "body") @RequestBody(required = true) ClassificationDetailVo classificationDetailVo) {
		log.debug("用户{}修改商品分类明细信息", HmSessionFactory.currentUserId());
		classificationDetailVo.setId(classificationDetailId);
		classificationService.updateClassificationDetail(classificationDetailVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value="/detail",method = RequestMethod.DELETE)
	@ApiOperation(tags = SWWAGER_TAG, value = "删除商品分类明细信息", httpMethod = "DELETE", notes = "com.hm.shopping.api.ClassificationApi::deleteClassificationDetail")
	public ResponseEntity<ServerResponse<Boolean>> deleteClassificationDetail(
			@ApiParam(required = true, name = "classificationDetailId", value = "商品分类ID") @RequestParam(required = true, name = "classificationDetailId") String classificationDetailId) {
		log.debug("用户{}删除商品分类明细信息", HmSessionFactory.currentUserId());
		classificationService.deleteClassificationDetail(classificationDetailId);
		return ControllerResult.success(true);
	}
	
}
